$(function(){
    var rjbys = {
        grid:$("#rjbys_grid"),
        gridOptions:{
            url:$.webPath() + "/cwgl/kmys/rjbysList",
            toolbar:'#toolbar_rjbys',
            fitColumns:true,
            fit:true,
            rownumbers:true,
            singleSelect:true,
            columns:[
                [
                    {field:'rjbys.type',title:'type',width:50,align:'center'},
                    {field:'rjbys.sbgsMc',title:'公司',width:100},
                    {field:'rjbys.kmdm',title:"科目代码",width:150},
                    {field:'rjbys.dm',title:"代码",editor:{type:'text'},width:90},
                    {field:'rjbys.fgf',title:"分隔符",editor:{type:'combobox',options:{
                        url: $.webPath() + '/base/sjzdsy/get_sjzdz?bm=RJBYSFGF',
                        valueField:'z',
                        textField:'z'
                    }},width:50,align:'center'},
                    {field:'cz',title:'操作',formatter:function(value,row,index){
                        if(value && value == 'save')
                            return '<a index="'+index+'" name="save" href="javascript:void(0)" onclick="save_rjbys('+index+')"></a>'
                        return '<a index="'+index+'" name="edit" href="javascript:void(0)" onclick="edit_rjbys('+index+')"></a>'
                    },width:30,align:'center'}
                ]
            ],
            onLoadSuccess:function(){
                $("a[name='edit']").linkbutton({
                    plain:true,
                    iconCls:'icon-edit'
                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onBeforeEdit:function(rowIndex, rowData){
                rjbys.grid.edatagrid("updateRow",{
                    index: rowIndex,
                    row:$.extend({},rowData,{cz:'save'})

                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onAfterEdit: function (index, row) {
                var url = $.webPath() +'/cwgl/kmys/save_rjbys';
                $.post(url, row, function (data) {
                    if(data.statusCode == 200){
                        rjbys.grid.datagrid('updateRow', {
                            index: index,
                            row:$.extend({},row,{cz:'edit'})
                        });
                        $("a[name='edit']").linkbutton({
                            plain:true,
                            iconCls:'icon-edit'
                        });
                    } else {
                        $.messager.alert('info',data.message);
                        rjbys.grid.edatagrid("beginEdit",index);
                    }
                }, 'json');
            }
        },
        search:function(){
            rjbys.grid.datagrid("load",{
                gs:$("#toolbar_rjbys [name='rjbys_gs']").val()
            });
        }
    }
    rjbys.grid.edatagrid(rjbys.gridOptions);

    $("#btn_rjbys_search").click(function(){
        rjbys.search();
    });
});
function save_rjbys(index){
    $("#rjbys_grid").edatagrid("endEdit",index);
}
function edit_rjbys(index){
    $("#rjbys_grid").edatagrid("beginEdit",index);
}